package com.stylefeng.guns.modular.domain_manage.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.domain_manage.model.DomainInfo;
import com.stylefeng.guns.modular.domain_manage.service.IDomainInfoService;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * 域名管理控制器
 *
 * @author fengshuonan
 * @Date 2020-06-29 15:27:47
 */
@Controller
@RequestMapping("/domainInfo")
public class DomainInfoController extends BaseController {

    private String PREFIX = "/domain_manage/domainInfo/";

    @Autowired
    private IDomainInfoService domainInfoService;

    /**
     * 跳转到域名管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "domainInfo.html";
    }

    /**
     * 跳转到添加域名管理
     */
    @RequestMapping("/domainInfo_add")
    public String domainInfoAdd() {
        return PREFIX + "domainInfo_add.html";
    }

    /**
     * 跳转到修改域名管理
     */
    @RequestMapping("/domainInfo_update/{domainInfoId}")
    public String domainInfoUpdate(@PathVariable Integer domainInfoId, Model model) {
        DomainInfo domainInfo = domainInfoService.selectById(domainInfoId);
        model.addAttribute("item",domainInfo);
        LogObjectHolder.me().set(domainInfo);
        return PREFIX + "domainInfo_edit.html";
    }

    /**
     * 获取域名管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return domainInfoService.getDomainOrderByStatus();
        //return domainInfoService.selectList(null);
    }

    /**
     * 新增域名管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DomainInfo domainInfo) {
        domainInfo.setCreateTime(new Date());
        domainInfo.setUrlName(StringUtils.trim(domainInfo.getUrlName()));
        domainInfoService.createNewDomain(domainInfo);
        return SUCCESS_TIP;
    }

    /**
     * 删除域名管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer domainInfoId) {
        domainInfoService.deleteById(domainInfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改域名管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DomainInfo domainInfo) {
        //domainInfoService.updateById(domainInfo);

        domainInfoService.updateDomainWithEntity(domainInfo);
        return SUCCESS_TIP;
    }

    /**
     * 域名管理详情
     */
    @RequestMapping(value = "/detail/{domainInfoId}")
    @ResponseBody
    public Object detail(@PathVariable("domainInfoId") Integer domainInfoId) {
        return domainInfoService.selectById(domainInfoId);
    }
}
